package com.sfh.cpc.note;

import android.content.Context;
import android.content.Intent;
import android.graphics.Color;
import android.os.Bundle;
import android.text.TextUtils;
import android.view.View;

import com.chad.library.adapter.base.BaseQuickAdapter;
import com.google.gson.Gson;
import com.jaeger.library.StatusBarUtil;
import com.sfh.cpc.R;
import com.sfh.cpc.com.BaseAppContent;
import com.sfh.cpc.com.entity.EMessage;
import com.sfh.cpc.com.entity.ENote;
import com.sfh.cpc.com.entity.EUser;
import com.sfh.cpc.com.entity.RspPage;
import com.sfh.cpc.com.entity.RxEventVip;
import com.sfh.cpc.com.refresh.SwipeRefreshMoreLayout;
import com.sfh.cpc.com.server.INoteServer;
import com.sfh.cpc.com.server.IUserServer;
import com.sfh.cpc.com.ui.VipLimit;
import com.sfh.cpc.com.widget.WidgetBaseToolbar;
import com.sfh.cpc.note.model.NoteModel;
import com.sfh.lib.AppCacheManager;
import com.sfh.lib.event.RxBusEvent;
import com.sfh.lib.mvvm.annotation.LiveDataMatch;
import com.sfh.lib.ui.AbstractLifecycleActivity;
import com.sfh.lib.ui.dialog.DialogBuilder;

/***
 * 内容详情
 */
public class NoteActivity extends AbstractLifecycleActivity<NoteModel> implements SwipeRefreshMoreLayout.OnLoadingListener {

    public static void startUI(Context context, ENote note) {

        Intent intent = new Intent(context, NoteActivity.class);
        intent.putExtra("DATA", note);
        context.startActivity(intent);
    }

    private WidgetBaseToolbar toolbar;
    private  View ly_content;
    private SwipeRefreshMoreLayout swipeRefreshMoreLayout;
    private MessageAdapter adapter;
    private NoteInfoView infoView;
    private int nowPage = 1;
    private ENote data;



    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.note_activity);
        StatusBarUtil.setColor(this, Color.WHITE, 0);
        ly_content = findViewById(R.id.ly_content);
        toolbar = findViewById(R.id.toolbar);
        adapter = new MessageAdapter();
        swipeRefreshMoreLayout = findViewById(R.id.swipeRefreshMoreLayout);
        swipeRefreshMoreLayout.setAdapter(adapter, true);
        swipeRefreshMoreLayout.addItemDecoration(R.dimen.px_1);
        swipeRefreshMoreLayout.setOnLoadingListener(this);
        swipeRefreshMoreLayout.setOnItemChildClickListener(new BaseQuickAdapter.OnItemChildClickListener() {
            @Override
            public void onItemChildClick(BaseQuickAdapter adapter, View view, int position) {
                EMessage message = (EMessage) adapter.getItem(position);
                INoteServer userServer = AppCacheManager.getCache(INoteServer.class.getSimpleName(), INoteServer.class);
                if (userServer != null) {
                    userServer.openUserNote(NoteActivity.this, message.getUserId());
                }
            }
        });
        swipeRefreshMoreLayout.setOnItemClickListener(new BaseQuickAdapter.OnItemClickListener() {
            @Override
            public void onItemClick(BaseQuickAdapter adapter2, View view, int position) {
                EMessage message = adapter.getItem(position);
                INoteServer userServer = AppCacheManager.getCache(INoteServer.class.getSimpleName(), INoteServer.class);
                if (userServer != null) {
                    userServer.openUserNote(NoteActivity.this, message.getUserId());
                }
            }
        });
        toolbar.setTitle("详情");
        data = getIntent().getParcelableExtra("DATA");
        if (data == null){
            return;
        }
        infoView = new NoteInfoView(this);
        adapter.addHeaderView(infoView);

        if (VipLimit.lookNote(this,data)){
            this.getViewModel().vipLookNote(data.getUserId());
            this.infoView.showData(data);
            this.ly_content.setVisibility(View.VISIBLE);
            this.swipeRefreshMoreLayout.onAutoRefresh();
        }else{
            ly_content.setVisibility(View.GONE);
        }
    }

    @RxBusEvent(from = "登录成功")
    public void onRxEventLogin(EUser user){
        if (TextUtils.equals("1",data.getNoteVip()) && (!user.isVip() && user.getUserId() != data.getUserId())){
            //要求VIP
            DialogBuilder builder = new DialogBuilder();
            builder.setMessage("需要VIP限制,激活VIP");
            builder.setHideCancel(true);
            builder.setOKTextListener("激活", new DialogBuilder.DialogInterface.OnClickListener() {
                @Override
                public void onClick(DialogBuilder.DialogInterface dialog, int which) {
                    dialog.dismiss();
                    IUserServer userServer = AppCacheManager.getCache(IUserServer.class.getSimpleName(), IUserServer.class);
                    if (userServer != null) {
                        userServer.openVip(NoteActivity.this,data.getNoteId(),1);
                    }
                }
            });
            showDialog(builder);
        }else{
            this.data.setNoteVip("0");
            this.getViewModel().vipLookNote(data.getUserId());
            this.infoView.showData(data);
            this.ly_content.setVisibility(View.VISIBLE);
            this.swipeRefreshMoreLayout.onAutoRefresh();
        }
    }

    @RxBusEvent(from = "用户 VIP 激活 or 用户已是VIP")
    public void onRxEventVIP(RxEventVip vip){
        this.getViewModel().vipLookNote(data.getUserId());
        this.data.setNoteVip("0");
        this.infoView.showData(data);
        this.ly_content.setVisibility(View.VISIBLE);
        this.swipeRefreshMoreLayout.onAutoRefresh();
    }


    @Override
    public void onRefreshUI() {
        this.nowPage = 1;
        this.getViewModel().pageMessage(nowPage, data.getNoteId());
    }

    @Override
    public void onLoadMoreUI() {
        this.nowPage++;
        this.getViewModel().pageMessage(nowPage, data.getNoteId());
    }

    @LiveDataMatch
    public void onPageSuccess(RspPage<EMessage> data) {

        if (data == null) {
            this.swipeRefreshMoreLayout.onLoadMoreFail();

        } else {
            nowPage = data.getPageNumber();
            swipeRefreshMoreLayout.onRefreshCompale(data.getList(), data.getPageNumber() != 1);
            if (data.getTotalPage() == data.getPageNumber()) {
                swipeRefreshMoreLayout.onLoadAllCompale();
            }
        }
    }


    public void onViewCilcked(View view) {
        final EUser user = AppCacheManager.getCache(BaseAppContent.USER, EUser.class);
        if (user == null || user.getUserId() <=0 ){
            //要求登录
            DialogBuilder builder = new DialogBuilder();
            builder.setMessage("请登录,去登录");
            builder.setHideCancel(true);
            builder.setOKTextListener("登录", new DialogBuilder.DialogInterface.OnClickListener() {
                @Override
                public void onClick(DialogBuilder.DialogInterface dialog, int which) {
                    dialog.dismiss();
                    IUserServer userServer = AppCacheManager.getCache(IUserServer.class.getSimpleName(), IUserServer.class);
                    if (userServer != null) {
                        userServer.openLogin(NoteActivity.this);
                    }
                }
            });
            showDialog(builder);
            return;
        }
        NoteAddMessageActivity.startUI(this, data.getNoteId(), data.getMassId());
    }

    @Override
    protected void onActivityResult(int requestCode, int resultCode, Intent data) {
        super.onActivityResult(requestCode, resultCode, data);
        if (requestCode == 1000 && resultCode == RESULT_OK){
            //留言成功
            String dara = data.getStringExtra("DATA");
            EMessage message =  new Gson().fromJson(dara,EMessage.class);
            adapter.addData(0,message);
            swipeRefreshMoreLayout.onLoadAllCompale();
            infoView.updateMsgSize();
        }
    }
}
